package com.yx.crawler.runtime;

import org.jsoup.Connection;
import org.jsoup.Connection.Method;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import com.yx.crawler.typedef.CrawlerPage;
import com.yx.crawler.typedef.CrawlerUrl;
import java.net.URLEncoder;
import java.util.Map;

public final class HttpProcessor {
	public static CrawlerPage process(CrawlerUrl crawlerUrl) throws Exception {
		Document document=null;
		Connection jsoupConnection=null;
		if (crawlerUrl.getParams() != null&&crawlerUrl.isGetMethod()) {
			String url=crawlerUrl.getUrl()+"?";
			for (Map.Entry<String, String> entry : crawlerUrl.getParams().entrySet()) {
				url=url+entry.getKey()+"="+URLEncoder.encode(entry.getValue(),"UTF-8")+"&";				
			}
			url=url.substring(0,url.length()-1);
			jsoupConnection=Jsoup.connect(url);			
		}else if(crawlerUrl.getParams() != null&&!crawlerUrl.isGetMethod()){
			jsoupConnection=Jsoup.connect(crawlerUrl.getUrl());
			for (Map.Entry<String, String> entry : crawlerUrl.getParams().entrySet()) {
				jsoupConnection.data(entry.getKey(), entry.getValue());
			}
		}else{
			jsoupConnection=Jsoup.connect(crawlerUrl.getUrl());
		}
		if(crawlerUrl.getUserAgent()!=null){
			jsoupConnection.userAgent(crawlerUrl.getUserAgent());
		}
		if(crawlerUrl.getCookie()!=null){
			jsoupConnection.cookies(crawlerUrl.getCookie());
		}
		jsoupConnection.timeout(5000);
		if (crawlerUrl.isGetMethod()) {
			jsoupConnection.method(Method.GET);			
		}else{
			jsoupConnection.method(Method.POST);			
		}
		Connection.Response response =jsoupConnection.execute();		
		CrawlerPage crawlerPage = new CrawlerPage(crawlerUrl, response);
		return crawlerPage;
	}
	public static CrawlerPage get(String url,Map<String, Object> meta) throws Exception {
		Connection jsoupConnection=Jsoup.connect(url);
		jsoupConnection.method(Method.GET);
		jsoupConnection.timeout(3000);
		Connection.Response response =jsoupConnection.execute();
		CrawlerUrl crawlerUrl=new CrawlerUrl(url,null);
		crawlerUrl.setMeta(meta);
		CrawlerPage crawlerPage = new CrawlerPage(crawlerUrl, response);
		return crawlerPage;
	}
	
}
